/* ozofra 1b
 This file contains the complete definition
 of the SecretCode class. */

#include <vector>
#include "d_except.h"
#include "vector_ops.h"
#include "d_random.h"

class SecretCode 
{
  public :
  SecretCode (int codeLength, int codeRange);
  void showAnswer () const;
  int checkCorrect(const std::vector<int> &guess) const; 
  int checkIncorrect(const std::vector<int> &guess) const;

 private:
  std::vector<int> secretValue;
};

SecretCode::SecretCode (int codeLength, int codeRange)
{
  randomNumber my_ran;

  //push random stuff n times
  for (int time=0; time<codeLength; time++)
  {
    int r_int = my_ran.random(codeRange);
    secretValue.push_back(r_int);
  }

}

void SecretCode::showAnswer () const
{
  std::cout << "Answer: " << secretValue << std::endl;
}

int SecretCode::checkCorrect(const std::vector<int> &guess) const
// Returns the number of correctDigits and correctLocations
// using the private member functions
{
  int correct=0;

  // loop over each guess element
  for (int gIdx=0; gIdx<guess.size(); gIdx++)
    // if identical to the secret, incr correct
    if (guess[gIdx] == secretValue[gIdx])
      correct++;

  return correct;
}

int SecretCode::checkIncorrect(const std::vector<int> &guess) const
// Returns the number of correctDigits and correctLocations
// using the private member functions
{
  int correctDigits = 0;
  vector<int> dSecret = secretValue;
  vector<int> dGuess = guess;
		
  // Remove those that are already correct
  // from future consideration
  for (int gIdx=0; gIdx<dGuess.size(); gIdx++)
    if (dGuess[gIdx] == secretValue[gIdx])
    {
      dSecret.at(gIdx) = -1;
      dGuess.at(gIdx) = -2;
    }

  // Loop over each guess
  for (int gIdx = 0; gIdx < dGuess.size(); gIdx++)
  {

    // Loop over each secret term
    for	(int idx = 0; idx < dSecret.size(); idx++)
    {	

      // If a secret term has been found, 
      // Increment correctDigits
      if (dSecret.at(idx) == dGuess.at(gIdx))
      {
	dSecret.at(idx) = -1;
	correctDigits++;
	break;
      }

    } //end secret for loop

  } //end guess for loop

  return correctDigits;
}
